package com.blb.dao.admin.impl;

import com.blb.dao.admin.ProductTypeDao;
import com.blb.pojo.ProductType;
import com.blb.utils.JDBCUtils;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class ProductTypeDaoImpl implements ProductTypeDao {

    @Override
    public ProductType selectByName(String name) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        ProductType productType = JDBCUtils.getOne(connection, ProductType.class, "select * from t_product_type where name = ?",name);
        connection.close();
        return productType;
    }

    @Override
    public List<ProductType> selectPage(int page, int size) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select * from t_product_type limit ?,?";
        List<ProductType> list = JDBCUtils.getList(connection, ProductType.class, sql,(page-1)*size,size);
        connection.close();
        return list;
    }

    @Override
    public int count() throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select count(id) count from t_product_type ";

        ResultSet resultSet = JDBCUtils.prepareStatement(connection, sql).executeQuery();
        resultSet.next();
        int count = resultSet.getInt("count");
        connection.close();
        return count;
    }

    @Override
    public void insert(ProductType productType) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection, "insert into t_product_type(name,icon,`desc`) values(?,?,?)",
                productType.getName(),productType.getIcon(),productType.getDesc()).executeUpdate();
        connection.close();
    }

    @Override
    public void deleteById(int id) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection, "delete from t_product_type where id = ?", id).executeUpdate();
        connection.close();
    }

    @Override
    public void batchDelete(String[] ids) throws SQLException, ClassNotFoundException {
        String sql = "delete from t_product_type where id in (";

        for (int i = 0; i < ids.length; i++) {
            sql+="?";
            if (i != ids.length-1){
                sql+=",";
            }
        }
        sql+=")";

        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection,sql,ids).executeUpdate();
        connection.close();
    }

    @Override
    public ProductType selectById(int id) throws Exception {
        Connection connection = JDBCUtils.getConnection();
        ProductType productType = JDBCUtils.getOne(connection, ProductType.class, "select * from t_product_type where id = ?", id);
        connection.close();
        return productType;
    }

    @Override
    public void update(ProductType productType) throws SQLException, ClassNotFoundException {
        Connection connection = JDBCUtils.getConnection();
        JDBCUtils.prepareStatement(connection,"update t_product_type set name=?,icon=?,`desc`=? where id=?"
                ,productType.getName(),productType.getIcon(),productType.getDesc(),productType.getId()).executeUpdate();
        connection.close();
    }

    @Override
    public List<ProductType> selectAll() throws Exception {
        Connection connection = JDBCUtils.getConnection();
        List<ProductType> list = JDBCUtils.getList(connection, ProductType.class, "select * from t_product_type");
        connection.close();
        return list;
    }
}
